/*
 * 文件名：merchantInfoController.java	 
 * 时     间：下午4:20:10
 * 作     者：liyf       
 * 版     权： 2014-2022  智慧园区, 公司保留所有权利.
 * 联     系：www.szyungu.com
 */
package com.cz073586.technology.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cz073586.common.contants.CommonContans;
import com.cz073586.common.controller.BaseController;
import com.cz073586.common.entity.pojo.PageInfo;
import com.cz073586.technology.model.AdInfo;
import com.cz073586.technology.model.MerchantAdInfo;
import com.cz073586.technology.model.MerchantInfo;
import com.cz073586.technology.model.MerchantType;
import com.cz073586.technology.service.face.AdInfoService;
import com.cz073586.technology.service.face.MerchantInfoService;
import com.cz073586.technology.service.face.MerchantTypeService;
import com.cz073586.tools.privilege.ResourceAndOperate;
import com.cz073586.tools.util.UUIDGenerateUtil;

/**
 * @ClassName: merchantInfoController
 * @Description: 商家信息
 * @author zengqj
 * @date 2015年1月20日 下午4:20:10
 */
@RestController
@RequestMapping(value = "/merchantManager", method = RequestMethod.POST)
@ResourceAndOperate(
		moduleID = "com.technology.merchantManager", 
		moduleName = "商家管理", 
		resouceDesc = "商家管理",
		parentModuleID = "com.technology", 
		parentModuleName = "昱显科技项目"
		)
public class MerchantInfoController extends BaseController
{
	@Autowired
	private MerchantInfoService merchantInfoService;
	
	@Autowired
	private AdInfoService adInfoService;
	
	@Autowired
	private MerchantTypeService merchantTypeService;
	/**
	 * 
	 * @方法名称: queryServicesByPage
	 * @描述: 查询商家列表
	 *
	 * @参数:
	 * @param page
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:zengqj
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryMerchantByPage.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.merchantManager.querymerchantListByPage", 
			moduleName = "查询", 
			resouceDesc = "查询分页对象",
			parentModuleID = "com.technology.merchantManager", 
			parentModuleName = "商家管理"
			)
	public ModelMap queryMerchantByPage(
			@RequestBody PageInfo<MerchantInfo> page)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			merchantInfoService.queryByPage(page);
			resultMap.put(CommonContans.STRUTS_DATA, page);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/queryMerchant.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.merchantManager.queryMerchant", 
			moduleName = "查询", 
			resouceDesc = "条件查询商店信息,入参：userKey;返回：商家所有信息（包括审核状态）.根据商家店铺名和地址以及主营业务匹配（serachBy）",
			parentModuleID = "com.technology.merchantManager", 
			parentModuleName = "商家管理"
			)
	public ModelMap queryMerchant(@RequestBody MerchantInfo merchantInfo)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			resultMap.put(CommonContans.STRUTS_DATA, merchantInfoService.queryMerchant(merchantInfo));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/queryMerchantSerachBy.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.merchantManager.queryMerchant", 
			moduleName = "查询", 
			resouceDesc = "条件查询商店信息,根据商家店铺名和地址以及主营业务匹配（serachBy）",
			parentModuleID = "com.technology.merchantManager", 
			parentModuleName = "商家管理"
			)
	public ModelMap queryMerchantSerachBy(@RequestBody MerchantInfo merchantInfo)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			resultMap.put(CommonContans.STRUTS_DATA, merchantInfoService.queryMerchantSerachBy(merchantInfo));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	
	/**
	 * 
	 * @方法名称: querymerchantById
	 * @描述: 通过商家ID查询
	 *
	 * @参数:
	 * @param merchantId
	 * @return
	 *
	 * @返回类型: ModelMap
	 * @作者:zengqj
	 * @可能抛出异常:
	 */
	@RequestMapping(value = "/queryMerchantById.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.merchantManager.queryMerchantById", 
			moduleName = "查询单一对象", 
			resouceDesc = "查询单一对象",
			parentModuleID = "com.technology.merchantManager", 
			parentModuleName = "商家管理"
			)
	public ModelMap queryMerchantById(String merchantId)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			
			resultMap.put(CommonContans.STRUTS_DATA,
					merchantInfoService.selectByPrimaryKey(merchantId));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	@RequestMapping(value = "/queryMerchantAd.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.merchantManager.queryMerchantAd", 
			moduleName = "查询", 
			resouceDesc = "入参：categoryId;cityId；返回：商家信息列表，以及和该子类型绑定的滑屏及词条广告的信息",
			parentModuleID = "com.technology.merchantManager", 
			parentModuleName = "商家管理"
			)
	public ModelMap queryMerchantAd(@RequestBody MerchantInfo merchantInfo)
	{
		ModelMap resultMap = new ModelMap();
		MerchantAdInfo MerchantAdInfo = new MerchantAdInfo();
		try
		{
			List<MerchantInfo> merchant = merchantInfoService.queryMerchant(merchantInfo);
			if(CollectionUtils.isNotEmpty(merchant))
			{
				
				MerchantType merchantType = new MerchantType();
				merchantType.setUuid(merchantInfo.getMerchantTypeKey());
				merchantType = merchantTypeService.selectByPrimaryKey(merchantInfo.getMerchantTypeKey());
				String[] adIds = merchantType.getBindToId().split(",");
				List<AdInfo> listAdInfo = new ArrayList<AdInfo>();
				for(String adId:adIds)
				{
					AdInfo adInfo = new AdInfo();
					adInfo = adInfoService.selectByPrimaryKey(adId);
					if(null!=adInfo)
					{
						listAdInfo.add(adInfo);
					}
				}
				if(CollectionUtils.isNotEmpty(listAdInfo))
				{
					MerchantAdInfo.setAdInfo(listAdInfo);
				}
			}
			MerchantAdInfo.setMerchant(merchant);
			resultMap.put(CommonContans.STRUTS_DATA,MerchantAdInfo);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：添加商家信息，uuid由代码写入
	 * 
	 * @Title: insertmerchant
	 * @param @param merchant
	 * @param @return
	 * @return ModelMap
	 * @merchant merchantministrator 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/insertMerchant.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.merchantManager.insertMerchant", 
			moduleName = "添加", 
			resouceDesc = "",
			parentModuleID = "com.technology.merchantManager", 
			parentModuleName = "商家管理"
			)
	public ModelMap insertMerchant(@RequestBody MerchantInfo merchant)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			merchant.setUuid(UUIDGenerateUtil.generate());
			//默认正在审核
			merchant.setAuditStatus(0);
			merchant.setCreateAt(new Date());
			resultMap.put(CommonContans.STRUTS_DATA,
					merchantInfoService.insert(merchant));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	
	
	/**
	 * 
	 * 方法描述：根据merchant主机修改商家信息
	 * 
	 * @Title: updatemerchant
	 * @param @param merchant
	 * @param @return
	 * @return ModelMap
	 * @merchant merchantministrator 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/updateMerchant.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.merchantManager.updateMerchant", 
			moduleName = "修改", 
			resouceDesc = "",
			parentModuleID = "com.technology.merchantManager", 
			parentModuleName = "商家管理"
			)
	public ModelMap updateMerchant(@RequestBody MerchantInfo merchant)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
 			if(null!=merchant&&null!=merchant.getTop()&&merchant.getTop()==0)
			{
				MerchantInfo merchantInfo= merchantInfoService.selectMaxTop(merchant);
				merchant.setTop(merchantInfo.getTop()+1);
			}else if(null!=merchant&&null!=merchant.getTop()&&merchant.getTop()!=0)
			{
				merchant.setTop(0);
			}
			int result = merchantInfoService.updateByPrimaryKeySelective(merchant);
			resultMap.put(CommonContans.STRUTS_DATA,result);
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
	
	/**
	 * 
	 * 方法描述：根据主键删除商家
	 * 
	 * @Title: deletemerchant
	 * @param @param merchantId
	 * @param @return
	 * @return ModelMap
	 * @merchant merchantministrator 2015年1月21日
	 * @throws
	 */
	@RequestMapping(value = "/deleteMerchant.action", consumes = "application/json", produces = "application/json")
	@ResponseBody
	@ResourceAndOperate(
			moduleID = "com.technology.merchantManager.deleteMerchant", 
			moduleName = "删除", 
			resouceDesc = "",
			parentModuleID = "com.technology.merchantManager", 
			parentModuleName = "商家管理"
			)
	public ModelMap deleteMerchant(String merchantId)
	{
		ModelMap resultMap = new ModelMap();
		
		try
		{
			resultMap.put(CommonContans.STRUTS_DATA,
					merchantInfoService.deleteByPrimaryKey(merchantId));
			resultMap.put(CommonContans.STRUTS_RESULT,
					CommonContans.STRUTS_SUCCESS);
		} catch (Throwable e)
		{
			// 请保持所有代码一直性，没有特殊处理请使用该代码.
			this.printExction(resultMap, e);
		}
		return resultMap;
	}
}
